const BASEURL = 'https://huangjiangjun.top:3002/api/'

function request({ url, method = 'GET', data = {}, header = {} }) {
  const token = wx.getStorageSync('token')
  if (token) {
    header.token = token
  }

  return new Promise((resolve, reject) => {
    wx.request({
      url: `${BASEURL}${url}`,
      method: method,
      data: data,
      header: header, // wx.request请求头没有s
      success: res => {
        if (res.statusCode === 401) { // token过期了
          // 清空token
          wx.removeStorageSync('token')

          // 调到登录页面
          wx.reLaunch({
            url: '/subpkg/login/login',
          })
        }
        // 传递的是响应体数据
        resolve(res.data)
      },
      fail: err => {
        reject(err)
      }
    })
  })
}

export default request